//
//  WYFollowMeController.m
//  Boss
//
//  Created by admin on 2018/8/23.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYFollowMeController.h"
#import "WYFollowMeCell.h"
#import "WYFollowMeModel.h"
#import "WYPersonWebHomePageController.h"
@interface WYFollowMeController ()<UITableViewDataSource,UITableViewDelegate,WYFollowMeCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSString *messageId;
@property (nonatomic)BOOL refresh;

@end

@implementation WYFollowMeController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        [weakSelf loadData];
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self clearCount];
}
-(void)clearCount
{
    [WYHttpServer cleanFollowMessageSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            
        }
        
        
    }];
    
    
    
}


-(void)loadData
{
    WEAKSELF
    [WYHttpServer seeFollowMsgWithMessageId:self.messageId WithPageSize:@(20) SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSArray *array =[WYFollowMeModel getFollowArrayFromJson:response[@"result"]];
            if (array.count) {
                [weakSelf.dataArray addObjectsFromArray:array];
                WYFollowMeModel *model = weakSelf.dataArray.lastObject;
                self.messageId = model.messageId;
            }
            [weakSelf.tableView reloadData];
            if (array.count) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
            
        }
        
        
    }];
    
    
    
    
}

-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYFollowMeCell" bundle:nil] forCellReuseIdentifier:@"WYFollowMeCell"];
    self.tableView.estimatedRowHeight = 100;
    self.navigationItem.title = @"关注我的";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYFollowMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYFollowMeCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYFollowMeCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    WYFollowMeModel *follow = self.dataArray[indexPath.row];
    [cell setFollow:follow];
    cell.delegate = self;
    
//    WYForwardModel *model = self.dataArray[indexPath.row];
//    [cell setFoeward:model];
    return cell;
    
}
#pragma mark  - WYFollowMeCellDelegate
-(void)clickStateAction:(WYFollowMeCell *)cell
{
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    WYFollowMeModel *userMessage =self.dataArray[indexPath.row];
    BOOL state =userMessage.attentionState.integerValue;
    state = !state;
//    if ([self.type isEqualToString:@"1"]) {
//        state = NO;
//    }
    WEAKSELF
    [WYHttpServer followUserOrUnFollowUser:userMessage.userId WithAttentionState:[NSString stringWithFormat:@"%d",state] SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            self->_refresh = YES;
            weakSelf.messageId = @"";
            [weakSelf loadData];
        }else{
            
        }
    }];
    
    
    
}
-(void)jumpToHomePageWithUserId:(NSString *)userId
{
    WYPersonWebHomePageController *pwc = [[WYPersonWebHomePageController alloc]init];
    pwc.urlString = userId;
    [self.navigationController pushViewController:pwc animated:YES];
    
    
    
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        _tableView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
    }
    return _tableView;
}


@end
